//
//  AppDelegate.swift
//  weibo
//
//  Created by yellow on 15/10/14.
//  Copyright © 2015年 yellow. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //从沙盒中加载用户信息到viewModel中
        
        setupWidgetTheme()
        observerForJumpVC()
        
        
        //因为我们使用代码编写界面，不使用sb，所以这是程序的入口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //设置根控制器为tabbar,tabbar里面包含navC
        window?.rootViewController = contextViewController()
        window?.makeKeyAndVisible()
        
        return true
    }
    
    
    /// 应用程序进入后台，清理数据缓存
    func applicationDidEnterBackground(application: UIApplication) {
        StatusDAL.clearCacheData()
    }
    
    deinit{
        //在析构函数中注销通知对象
        NSNotificationCenter.defaultCenter().removeObserver(self, name: "jump2MainVCNotification", object: nil)
    }
    
}

// MARK: - 私有方法
extension AppDelegate{
    
    //根据上下文返回对应的控制器
    private func contextViewController()->UIViewController{
        
        if UserAccountViewModel.sharedViewModel.isLoginUser {
            
            return isNewVerion() ? NewFeatureViewController():WelcomeViewController()
        }else{
            return MainViewController()
        }
    }
    
    //判断是否为新版本，或是第一次安装
    private func isNewVerion() ->Bool {
        //读取info.plist文件中的版本号,用户偏好设置verison的key需要加前缀，以防和系统的key冲突
        
        let lastVersion = NSUserDefaults.standardUserDefaults().doubleForKey("weiboVerison")

        print("上个版本号:\(lastVersion)")
        
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        print("当前版本号:\(currentVersion)")
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: "weiboVerison")
        NSUserDefaults.standardUserDefaults().synchronize()

        
       
        
        if lastVersion < currentVersion {
            print("升级为为新版本，展示新特性")
            return true
        }else{
            return false
        }
    }
    
    //设置导航控制器以及选项卡控制器的tintColor为橘色
    private func setupWidgetTheme(){
        // 修改导航栏的全局外观 － 要在控件创建之前设置，一经设置全局有效
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    }
    
    
    //监听自定义跳转控制器通知,传递目标控制器的className
    func observerForJumpVC(){
    
        NSNotificationCenter.defaultCenter().addObserverForName(jump2MainVCNotification, object: nil, queue: nil) { (notification) -> Void in
            let currentVC = notification.object as! String
            switch currentVC{
                case "MainViewController":
                    self.window?.rootViewController = MainViewController()
                case "NewFeatureViewController":
                    self.window?.rootViewController = NewFeatureViewController()
                case "WelcomeViewController":
                    self.window?.rootViewController = WelcomeViewController()
                default:break
            }
        }
    }
    
   

}


